<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zend_View **/
require_once 'Zend/View.php';

/** @see Zend_Form **/
require_once 'Zend/Form.php';

/** @see Zead_Quiz_Question_Abstract **/
require_once 'Zead/Quiz/Question/Abstract.php';

/**
 * Class Zead_Quiz 
 * 
 * @category   Zead   
 * @package    Zead_Quiz
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License   
 */
class Zead_Quiz
{
	/** @var Zend_Form **/
	protected $_form = null;
	
	/** @var string **/
	protected $_title = null;
	
	/** @var string **/
	protected $_description = null;
	
	/** @var array **/
	protected $_questions = array();

	/**
	 * Class constructor
	 *
	 * @param string $name
	 */
	public function __construct($title = null, $desc = null, array $questions = array())
	{
		// Sets title
		if (null !== $title) {
			$this->_title = $title;
		}
		// Sets desc
		if (null !== $desc) {
			$this->_description = $desc;
		}
			
		//$this->_form = new Zend_Form();
	}
	
	/**
	 * Render the quiz
	 *
	 * @param
	 */
	public function render()
	{
		$this->_view = new Zend_View();
		$this->_view->doctype('XHTML1_TRANSITIONAL');		
		$this->_form = new Zend_Form();
		$this->_form->setView($this->_view);
		
		foreach ($this->_questions as $question) {
			// Multiple Choice Question
			if ($question instanceof Zead_Quiz_Question_MultipleChoice) {
				$element = $this->_form->createElement('multiCheckbox', '_field' . uniqid(), array('label' => $question->getTitle()));
				
				foreach ($question->getResponses() as $response) {
					$element->addMultiOption($response['label'], $response['value']);
				}
				$this->_form->addElement($element);
			// Single Text question	
			} else if ($question instanceof Zead_Quiz_Question_SingleChoice) {
				$element = $this->_form->createElement('radio', '_field' . uniqid(), array('label' => $question->getTitle()));
				
				foreach ($question->getResponses() as $response) {
					$element->addMultiOption($response['label'], $response['value']);
				}
				$this->_form->addElement($element);			
			} else {	
				$this->_form->addElement('text', '_field' . uniqid(), array('label' => $question->getTitle()));
			}
		}
		
		return $this->_form->render();		
	}
	
	/**
	 * Sets the question title
	 * @param string $title
	 */
	public function setTitle($title)
	{
		$this->_title = $title;
		return $this;
	}
	
	/**
	 * Returns question title
	 * @return string $title
	 */
	public function getTitle()
	{
		return $this->_title;
	}
	
	/**
	 * Sets the question description
	 * @param string $description
	 */
	public function setDescription($description)
	{
		$this->_description = $description;
		return $this;
	}
	
	/**
	 * Returns the description
	 * @return string
	 */
	public function getDescription()
	{
		return $this->_description;
	}	
	
	
	/**
	 * Ads a question to quiz
	 *
	 * @param Zead_Quiz_Question_Abstract|string
	 * @param string $title
	 * @param string $desc
	 * @param array  $responses
	 */
	public function addQuestion($question, $title, $desc, array $responses = array())
	{
		if (!$question instanceof Zead_Quiz_Question_Abstract) {
			require_once 'Zend/Filter.php';
			$className = 'Zead_Quiz_Question_' . Zend_Filter::get($question, 'Word_DashToCamelCase');
			//require_once str_replace('_', '/', $className) . '.php';
			$question = new $className($title, $desc, $responses);
		}
		$this->_questions[] = $question;
		
		return $question;
	}
	
	/**
	 * Create a question object
	 *
	 * @param string $title
	 * @param string $desc
	 * @param array  $responses	 
	 * @return Zead_Quiz_Question
	 */
	public function createQuestion($type, $title, $desc, $responses = array(), array $options = array())
	{
		return new Zead_Quiz_Question_MultipleChoice($title, $desc);
	}
	
	/**
	 * Return the Zend_Form object
	 *
	 * @return Zend_Form
	 */
	protected function _getForm()
	{
		return $this->_form;
	}	
}
